﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Artisteer
{
    public class Menu : HierarchicalDataBoundControl
    {
        public bool ShowRootNode { get; set; }

        public Menu()
        {
            ShowRootNode = true;
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
        }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            DataBind();

            SiteMapDataSource siteMap = GetDataSource() as SiteMapDataSource;
            SiteMapProvider provider = siteMap.Provider;

            if (siteMap != null && siteMap.Provider != null)
            {
                if (ShowRootNode)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    renderSiteMapNode(writer, provider, provider.RootNode, 0, provider.CurrentNode == provider.RootNode);
                    writer.RenderEndTag();
                }

                renderSiteMapNodes(writer, provider, provider.RootNode, 0, false);
            }
        }

        protected void renderSiteMapNodes(HtmlTextWriter writer, SiteMapProvider provider, SiteMapNode parentNode, int level, bool isActive)
        {
            if (parentNode.ChildNodes.Count > 0)
            {
                SiteMapNode activeNode = null;

                foreach (SiteMapNode node in parentNode.ChildNodes)
                {
                    for (activeNode = provider.CurrentNode; activeNode != null; activeNode = activeNode.ParentNode)
                        if (node == activeNode) break;

                    if (activeNode != null) break;
                }

                if (level > 0)
                {
                    if (isActive) writer.AddAttribute(HtmlTextWriterAttribute.Class, "active");

                    writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                }

                foreach (SiteMapNode node in parentNode.ChildNodes)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    renderSiteMapNode(writer, provider, node, level, node == activeNode);
                    renderSiteMapNodes(writer, provider, node, level + 1, node == activeNode);
                    writer.RenderEndTag();
                }

                if (level > 0) writer.RenderEndTag();
            }
        }

        protected void renderSiteMapNode(HtmlTextWriter writer, SiteMapProvider provider, SiteMapNode node, int level, bool isActive)
        {
            if (isActive)
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "active");

            writer.AddAttribute(HtmlTextWriterAttribute.Href, node.Url);

            writer.RenderBeginTag(HtmlTextWriterTag.A);

            if (level == 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "l");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "r");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.RenderEndTag();

                writer.AddAttribute(HtmlTextWriterAttribute.Class, "t");
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(node.Title);
                writer.RenderEndTag();
            }
            else
            {
                writer.Write(node.Title);
            }

            writer.RenderEndTag();
        }
    }
}